/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package biofilter.util;

import biofilter.exceptions.InvalidRecordException;
import biofilter.filters.*;
import biofilter.records.FileNameRecord;
import biofilter.records.Record;
import biofilter.sources.RecordsFile;
import biofilter.sources.RecordsSourceIF;
import java.util.Vector;

/**
 * It can be quite confusing how to properly setup a source in pipes and filters.
 * Usually, once the source is set, adding filters and building pipelines is gravy.
 * Often one forgets what classes are needed and what params they take.
 * This class serves to document some common use cases.  Feel free to 
 * call the functions directly if they serve you.
 * 
 * @author Daniel Quest
 * @date   April 14th 2010
 */
public class SourceUtil {


    /**if you want a single line record source you may want to do something like:
     * RecordsFile r = new RecordsFile(this.annotationFile, "biofilter.records.GFFRecord");
     *
     * Then you may be able to use it like this:
     *  while(!this.annGFF.eof()){
     *      GFFRecord gff = (GFFRecord) this.annGFF.getRecord();
     *      System.err.println(gff.get());
     *  }
     */

    /**
     *
     * createSource and bindSources are basically the same function.
     * createSources takes a vector containing paths to files for processing.
     * createSource takes a String that is a single path to a file for processing.
     *
     * Note, the paths passed to these functions hold DATA, not paths to files.
     * The DATA is assumed to be single line records for these functions to
     * correctly bind the source to the filter.
     *
     * In both cases, bindSource will take the paths, automatically setup a source
     * for the given file paths, and it will bind the source to the provided filter, f
     *
     * Return f
     * You will need to cast F to the type of filter passed in.
     *
     * use:
     * RecordType r = (RecordType) f.getRecord();
     * e.g.
     * BowTieRecord result = (BowTieRecord) rf2br.getRecord();
     */
    public RecordsFile createSource(String sourceFileName) throws InvalidRecordException{
        Vector<String> paths = new Vector();
        paths.add(sourceFileName);
        return createSources(paths);
    }
        

    public RecordsFile createSources(Vector<String> v) throws InvalidRecordException{
        ArrayUtil aute = new ArrayUtil();
        Vector<Record> rv = new Vector<Record>();
        for(int i=0; i<v.size(); i++){
            FileNameRecord fnr = new FileNameRecord(); //this is not general... the goal here is to simplify things for single line records
            String path = v.get(i);
            fnr.set(path);
            rv.add(fnr);
        }
        Record [] ra = aute.toArray(rv);
        //aute.printArray(ra);
        RecordsFile rf = new RecordsFile(ra, "biofilter.records.FileNameRecord");
        //System.out.println("TEST:" + rf.getRecord().get());
//        f.setSource(rf);
        return rf;
    }

}
